﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RoleMgr : BaseManager<RoleMgr>
{
    public int _mainRoleThisID;

    private MainRole _mainRole;
    public MainRole MainRole { get { return _mainRole; } }
    //ThisID,role
    public Dictionary<int, Role> AllRole = new Dictionary<int, Role>();

    internal static void OnMainRoleThisID(Cmd cmd)
    {
        if (!Client.CherkCmd(cmd, typeof(MainRoleThisIdCmd)))
            return;
        MainRoleThisIdCmd thisIdCmd = cmd as MainRoleThisIdCmd;
        RoleMgr.GetInstance()._mainRoleThisID = thisIdCmd.thisID;
    }

    internal static void OnCreateSenceRoleCmd(Cmd cmd)
    {
        if (!Client.CherkCmd(cmd, typeof(CreateSenceRoleCmd)))
            return;
        CreateSenceRoleCmd createRole = cmd as CreateSenceRoleCmd;
        //Debug.Log(createRole.ModelID);
        //Debug.Log(thisRoleCmd);
        RoleDatabase roleDatabase = RoleTable.GetInstance()[createRole.ModelID];
        if (roleDatabase == null)
        {
            Debug.LogError("未找到角色模型"+createRole.ModelID);
        }
        //创建角色模型
        var roleObj = ResManager.GetInstance().load<GameObject>(roleDatabase.ModelPath);
        Role role;
        //判断是否是主角
        if (RoleMgr.GetInstance()._mainRoleThisID == createRole.ThisID)
        {
            GetInstance()._mainRole = roleObj.AddComponent<MainRole>();
            role = GetInstance()._mainRole; 
            CameraControll.Instance.m_followTarget = role.transform;
        }
        else
        {
            role = roleObj.AddComponent<Role>();
        }
        
        role.Init(createRole,roleDatabase);
        //把角色ID和角色脚本记录下来
        RoleMgr.GetInstance().AllRole[createRole.ThisID] = role;
        
    }

    internal void Reset()
    {
        foreach(var role in RoleMgr.GetInstance().AllRole)
        {
            ResManager.GetInstance().Release(role.Value.gameObject);
        }
        RoleMgr.GetInstance().AllRole.Clear();

    }
}
